﻿using AForge.Neuro;
using AForge.Neuro.Learning;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Lung_Sounds_V2
{
    /// <summary>
    /// Interaction logic for AnalyseWindow.xaml
    /// </summary>
    public partial class AnalyseWindow : Window
    {
        private List<List<double[]>> _snippets;
        private List<double[]> _currentWindow;

        public AnalyseWindow(List<List<double[]>> snippets)
        {
            _snippets = snippets;
            InitializeComponent();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            new Thread(new ThreadStart(Train)).Start();
        } 

        private void Train()
        {
            // pattern size
            int patternSize = 30;
            // patterns count
            int patterns = 2;

            // learning input vectors
            double[][] input = new double[2][]
            {
                new double[] {
                    0.5f, -0.5f, -0.5f,  -0.5f,  -0.5f,
                    0.5f, -0.5f,  -0.5f, -0.5f, -0.5f,
                    0.5f,  -0.5f, -0.5f, -0.5f, -0.5f,
                    0.5f, -0.5f,  -0.5f, -0.5f, -0.5f,
                    0.5f, -0.5f, -0.5f,  -0.5f, -0.5f,
                    0.5f, 0.5f, 0.5f, 0.5f,  0.5f}, // L

                new double[] {
                    0.5f, -0.5f, -0.5f,  0.5f,  0.5f,
                    0.5f, -0.5f,  0.5f, -0.5f, -0.5f,
                    0.5f,  0.5f, -0.5f, -0.5f, -0.5f,
                    0.5f, -0.5f,  0.5f, -0.5f, -0.5f,
                    0.5f, -0.5f, -0.5f,  0.5f, -0.5f,
                    0.5f, -0.5f, -0.5f, -0.5f,  0.5f}, // Letter K
            };
            // learning ouput vectors
            double[][] output = new double[2][]
            {
                 new double[] {
                    -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
                    -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
                    -0.5f,  0.5f, -0.5f, -0.5f, -0.5f,
                    -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
                    -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
                    -0.5f}, // L

                new double[] {
                        -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
                        -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
                         0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
                        -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
                        -0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
                        -0.5f},                // Letter K
            };

            // create neural network
            ActivationNetwork neuralNet = new ActivationNetwork(new BipolarSigmoidFunction(2.0f),
                                                     patternSize, patterns);
            // randomize network`s weights
            neuralNet.Randomize();

            // create network teacher
            AForge.Neuro.Learning.BackPropagationLearning teacher = new
                AForge.Neuro.Learning.BackPropagationLearning(neuralNet);
            //teacher.LearningLimit    = 0.1f;
            teacher.LearningRate = 0.5f;
            

            // teach the network
            int i = 0;
            do
            {
                i++;
            }
            while (teacher.RunEpoch(input, output) < 0.1f);

            //
            System.Diagnostics.Debug.WriteLine("total learning " +
                                                    "epoch: " + i);
        }
    }
}
